﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using WindowsFormsApplication1.ServiceReference1;
using System.ServiceModel;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form, IService1Callback,IDisposable
    {
        private Service1Client _svc;
        string keyClientProxy;

        public Form1()
        {
            InitializeComponent();
            _svc = new Service1Client(new InstanceContext(this));
            keyClientProxy = _svc.RegisterClient();
            GetDataChat();
        }

        public void GetDataChat()
        {
            _svc = new Service1Client(new InstanceContext(this));
            List<string> listChat = _svc.GetDataChat();
            listBox1.DataSource = listChat;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            _svc = new Service1Client(new InstanceContext(this));
            _svc.AddText(textBox1.Text);
            textBox1.Clear();
        }

        public void RefreshData()
        {
            _svc = new Service1Client(new InstanceContext(this));
            List<string> listChat = _svc.GetDataChat();
            listBox1.DataSource = listChat;
            listBox1.SelectedIndex = listChat.Count - 1;
            listBox1.ClearSelected();
        }

        private void Form1_FormClosed(object sender, FormClosedEventArgs e)
        {
            _svc.RemoveClient(keyClientProxy);
            _svc.Close();
        }
    }
}
